package com.peng.herostory.cmdhanlder;

import com.peng.herostory.msg.GameMsgProtocol;
import com.peng.herostory.rank.RankItem;
import com.peng.herostory.rank.RankService;
import io.netty.channel.ChannelHandlerContext;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class RankCmdHandler implements ICmdHandler<GameMsgProtocol.GetRankCmd> {
    @Override
    public void handle(ChannelHandlerContext ctx, GameMsgProtocol.GetRankCmd msg) {
        RankService.getInstance().getRank((rankItems -> {
            log.info("rankItems;{}", rankItems);
            GameMsgProtocol.GetRankResult.Builder builder = GameMsgProtocol.GetRankResult.newBuilder();
            for (RankItem rankItem : rankItems) {
                GameMsgProtocol.GetRankResult.RankItem.Builder itemBuilder = GameMsgProtocol.GetRankResult.RankItem.newBuilder();
                itemBuilder.setUserId(rankItem.userId);
                itemBuilder.setRankId(rankItem.rankId);
                itemBuilder.setWin(rankItem.win);
                itemBuilder.setUserName(rankItem.userName);
                itemBuilder.setHeroAvatar(rankItem.heroAvatar);
                builder.addRankItem(itemBuilder);
            }
            GameMsgProtocol.GetRankResult rankResult = builder.build();
            ctx.writeAndFlush(rankResult);
        }));

    }


}
